﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="CreateFirewallRuleCommand.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagement.SqlAzureCommands
{
    using System;

    using AzureManagement.AzureCommands;
    using AzureManagement.SqlAzureDataContracts;

    using Common;

    using RestClient;

    public class CreateFirewallRuleCommand : AzureManagementCommand
    {
        private readonly string serverName;

        private readonly string ruleName;

        private readonly FirewallRule rule;

        public CreateFirewallRuleCommand(IAzureServiceContext serviceContext, string serverName, string ruleName, FirewallRule rule)
            : base(serviceContext)
        {
            if (string.IsNullOrEmpty(serverName))
            {
                throw new ArgumentException("Server name must not be null or empty.");
            }

            if (string.IsNullOrEmpty(ruleName))
            {
                throw new ArgumentException("Rule name must not be null or empty.");
            }

            if (rule == null)
            {
                throw new ArgumentException("Firewall rule must not be null.");
            }

            this.serverName = serverName;
            this.ruleName = ruleName;
            this.rule = rule;
        }

        protected override RequestContext GetRequestContext()
        {
            return this.CreatePutOperationRequestContext();
        }

        protected override string FormatPath(IAzureServiceContext azureServiceContext)
        {
            return string.Format(
                            "{0}/servers/{1}/firewallrules/{2}",
                            azureServiceContext.SubscriptionId,
                            this.serverName,
                            this.ruleName);
        }

        protected override string FormatBody()
        {
            return XmlHelper.Serialize(this.rule);
        }
    }
}